package com.vdm.action;

import com.vdm.model.UserInfo;
import com.vdm.service.UserInfoService;
import com.vdm.util.Constants;
import com.vdm.util.ErrorConstants;
import com.vdm.util.ReturnMapUtil;
import com.vdm.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    UserInfoService userInfoService;

    @RequestMapping(value = "/toLogin",method = RequestMethod.GET)
    public String toLogin(){
        return "login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String pcLogin(String userName, String password, String code, HttpSession session) {
        String sessionCode = (String) session.getAttribute(Constants.SESSION_LOGIN_CODE);
        System.out.println(userName);
        if (!StringUtil.isNullOrEmpty(userName) && !StringUtil.isNullOrEmpty(password)
                && !StringUtil.isNullOrEmpty(sessionCode) && sessionCode.equalsIgnoreCase(code)) {
            // 通过用户名查找用户
            UserInfo user = userInfoService.getUserByLoginName(userName);
            System.out.println(user);
            if (user != null) {
                System.out.println(user.getLoginName());
                if (user.getPwd().equals(password) && user.getStatus() == 1) {
                    session.setAttribute(Constants.CURRENT_USER, user);
                    return "redirect:toUserList";
                }
            }
        }
        return "login";
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String toLogout(HttpSession session) {
        session.removeAttribute(Constants.CURRENT_USER);
        return "login";
    }

    @RequestMapping(value = "/toUserList")
    public String toUserList(){return "userList";}

    @ResponseBody
    @RequestMapping(value = "/usersList",method = RequestMethod.GET)
    public Map<String,Object> userList(){
        List<UserInfo> userList = userInfoService.userList();
        int count = userInfoService.getUserCount();
        Map<String,Object> retMap = new HashMap<String,Object>();
        retMap.put("code",0);
        retMap.put("msg","");
        retMap.put("count",count);
        retMap.put("data",userList);
        return retMap;
    }

    @ResponseBody
    @RequestMapping(value="/updateUserStatus",method=RequestMethod.POST)
    public Map<String,Object> updateUserStatus(@RequestParam(value = "list[]")List<Integer> ids, Integer type){
        if(userInfoService.updateUserStatus(ids, type)>0){
            return ReturnMapUtil.getOKResult();
        }else{
            return ReturnMapUtil.getErrorResult(ErrorConstants.PARAMETER_ERROR);
        }
    }

    @ResponseBody
    @RequestMapping(value="/updateUserByPk",method=RequestMethod.POST)
    public Map<String,Object> updateUserByPk(UserInfo user){
        if(userInfoService.updateByPk(user)>0){
            return ReturnMapUtil.getOKResult();
        }else{
            return ReturnMapUtil.getErrorResult(ErrorConstants.PARAMETER_ERROR);
        }
    }

    @RequestMapping(value = "/toAddUser")
    public String toAddUser(){return "addUser";}

    @RequestMapping(value = "/addUser" , method = RequestMethod.POST)
    public String addUser(UserInfo user){
        userInfoService.insertSelective(user);
        return "redirect:toUserList";
    }

}
